<?php

declare (strict_types=1);
namespace App\Model;

use App\Traits\ModelLabelTrait;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property int $user_id 
 * @property int $dataable_id 
 * @property string $dataable_type 
 * @property int $status 
 * @property string $opera_time 
 * @property string $content 
 * @property string $pictures 
 * @property string $mask 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property string $deleted_at 
 */
class Complaint extends Model
{
    use SoftDeletes,ModelLabelTrait;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'complaint';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'type',
        'user_id',
        'status',
        'content',
        'pictures'
    ];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'user_id' => 'integer', 'dataable_id' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];

    protected $appends=[
        'type_label',
        'status_label'
    ];

    const TYPE_JOB = 1;
    const TYPE_USER = 2;

    const TYPE_LABELS=[
        self::TYPE_JOB => '岗位',
        self::TYPE_USER => '用户'
    ];

    const STATUS_DEFAULT = 0;
    const STATUS_SUCCESS = 1;
    const STATUS_FAIL = -1;

    const STATUS_LABELS=[
        self::STATUS_DEFAULT => '待审核',
        self::STATUS_SUCCESS => '审核通过',
        self::STATUS_FAIL => '审核未通过'
    ];

    public function getTypeLabelAttribute(){
        return self::getLabel('type',$this->type);
    }

    public function getStatusLabelAttribute(){
        return self::getLabel('status',$this->status);
    }

    public function getPicturesAttribute($field){
        $pictures = $this->attributes[$field] = $field?explode(',',$field):[];
        $pictures = collect($pictures)->map(function($item){
            $item= config('common.image_prefix').$item;
            return $item;
        });
        return $pictures;
    }

    public function dataable(){
        return $this->morphTo();
    }
}